package com.aaa.system.service.impl;

import com.aaa.entity.RegisteredItem;
import com.aaa.entity.User;
import com.aaa.system.dao.RegisteredItemDao;
import com.aaa.system.dao.UserDao;
import com.aaa.system.service.IRegisteredItemService;
import com.aaa.utils.JWTUtils;
import com.aaa.utils.WebUtil;
import com.aaa.vo.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@Service
public class RegisteredItemServiceImpl extends ServiceImpl<RegisteredItemDao, RegisteredItem> implements IRegisteredItemService {

    @Resource
    private RegisteredItemDao registeredItemDao;

    @Resource
    private UserDao userDao;

    @Override
    public Result<List<RegisteredItem>> getAll() {
        QueryWrapper<RegisteredItem> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status",0);
        return new Result<>(200,"成功查询所有",registeredItemDao.selectList(queryWrapper));
    }

    @Override
    public Result<IPage<RegisteredItem>> selectAll(Integer current, Integer size, RegisteredItem registeredItem) {
        IPage<RegisteredItem> page = new Page<>(current,size);
        QueryWrapper<RegisteredItem> queryWrapper = new QueryWrapper<>();
        if (StringUtils.hasText(registeredItem.getRegItemName())){
            queryWrapper.like("reg_item_name",registeredItem.getRegItemName());
        }
        if (Objects.nonNull(registeredItem.getStatus())){
            queryWrapper.eq("status",registeredItem.getStatus());
        }
        queryWrapper.eq("del_flag","0");
        IPage<RegisteredItem> registeredItemIPage = registeredItemDao.selectPage(page,queryWrapper);
        return new Result<>(200,"查询所有挂号项目成功",registeredItemIPage);
    }

    @Override
    public Result<Boolean> saveOrUpdateRegItem(RegisteredItem registeredItem) {
        //直接在token获取手机号
        HttpServletRequest request = WebUtil.getRequest();
        String token = request.getHeader("token");
        Map<String, Object> tokenInfo = JWTUtils.getInfo(token);
        String phone = tokenInfo.get("username").toString();
        //通过token获取登录用户信息
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("phone",phone);
        User userByToken = userDao.selectOne(queryWrapper);
        if (Objects.nonNull(registeredItem.getRegItemId())){
            //如果有挂号项目id，执行的就是修改操作
            registeredItem.setUpdateTime(LocalDateTime.now());
            registeredItem.setUpdateBy(userByToken.getUserName());
            int i = registeredItemDao.updateById(registeredItem);
            return new Result<>(200,"修改挂号费用成功",i>0?true:false);
        }else {
            //如果有挂号项目id，执行的就是添加操作
            registeredItem.setCreateTime(LocalDateTime.now());
            registeredItem.setCreateBy(userByToken.getUserName());
            int insert = registeredItemDao.insert(registeredItem);
            return new Result<>(200,"添加挂号费用成功",insert>0?true:false);
        }
    }

    @Override
    public Result<Boolean> deleteRegItemByIds(Long[] regItemIds) {
        Boolean b = null;
        if (regItemIds.length>0){
            b = registeredItemDao.batchDelById(regItemIds);
        }
        return new Result<>(200,"删除成功",b);
    }
}
